package com.example.ycdyus.dailyfruit.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.constant.AppConfig;
import com.example.ycdyus.dailyfruit.db.User;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

@EActivity(R.layout.activity_login)
public class LoginActivity extends AppCompatActivity {

    @ViewById
    EditText etLoginName;
    @ViewById
    EditText etLoginPsw;
    @ViewById
    Button btnLoginLogin;
    @ViewById
    Button btnLoginRegister;
    @ViewById
    TextView tvLoginForgetpsw;
    @ViewById
    ImageView ivLoginWeibo;
    @ViewById
    ImageView ivLoginQq;
    @ViewById
    ImageView ivLoginWechat;


    private String id;
    private String userIcon;
    private String name;

    @AfterViews
    public void init(){
        Platform weibo = ShareSDK.getPlatform(this, QQ.NAME);
        weibo.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                name = (String) hashMap.get("nickname");
                userIcon = (String) hashMap.get("figureurl_qq_2");
                id = System.currentTimeMillis()+"";
                Log.e("HHHH",name);
                Toast.makeText(LoginActivity.this, "验证"+name, Toast.LENGTH_SHORT).show();
                etLoginName.setText(name);
                LoginActivity.this.finish();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        weibo.showUser(null);//执行登录，登录后在回调里面获取用户资料


//        Toast.makeText(LoginActivity.this, "HAHAHAHAHAHA", Toast.LENGTH_SHORT).show();
    }

    /**
     * 第三方登陆实现方法
     * 1> 获取平台
     */
    public void threeLogin(Platform platform) {
        platform.removeAccount();
        platform.removeAccount(true);
        if (platform == null) {
            return;
        }
        PlatformDb db = platform.getDb();
        if (db.isValid()) {
            //一种已经认证过并且没过期
            String id = db.getUserId();
            String name = db.getUserName();
            String url = db.getUserIcon();
            Toast.makeText(LoginActivity.this, "验证"+name+"000", Toast.LENGTH_SHORT).show();
            return;
        }

        /**
         *没有认证
         */
        platform.setPlatformActionListener(new PlatformActionListener() {



            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.e("HHHH",hashMap.toString());
                if(platform.getName().equals(QQ.NAME)){
                    name = (String) hashMap.get("nickname");
                    userIcon = (String) hashMap.get("figureurl_qq_2");
                    id = System.currentTimeMillis()+"";
                }else if(platform.getName().equals(Wechat.NAME)){
                    //微信

                }else if(platform.getName().equals(SinaWeibo.NAME)){
                    //微博
                    name = (String) hashMap.get("name");
                }
                Log.e("HHHH",name);
                Toast.makeText(LoginActivity.this, "验证"+name, Toast.LENGTH_SHORT).show();
                etLoginName.setText(name);
                LoginActivity.this.finish();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Toast.makeText(LoginActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Toast.makeText(LoginActivity.this, "取消验证", Toast.LENGTH_SHORT).show();  
            }
        });
        platform.showUser(null);
    }

    /**
     * 调用本地的登陆方法请求自己的服务器
     * @param name
     * @param id
     */
    public void login(String name, String id) {

        new Thread() {
            @Override
            public void run() {
                //如果成功 直接服务器返回用户信息  未绑定本地用户系统

                //失败根据相应服务器的状态吗处理
            }
        }.start();


    }


    @Click({R.id.btn_login_login,R.id.btn_login_register,R.id.tv_login_forgetpsw,R.id.iv_login_weibo,R.id.iv_login_wechat,R.id.iv_login_qq})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_login_login:
                Toast.makeText(LoginActivity.this, "btn_login_login", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_login_register:
                Toast.makeText(LoginActivity.this, "btn_login_register", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_weibo:
                threeLogin(ShareSDK.getPlatform(SinaWeibo.NAME));
                Toast.makeText(LoginActivity.this, "iv_login_weibo", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_wechat:
                threeLogin(ShareSDK.getPlatform(Wechat.NAME));
                Toast.makeText(LoginActivity.this, "iv_login_wechat", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_qq:
                threeLogin(ShareSDK.getPlatform(QQ.NAME));
                LoginActivity.this.finish();
                Toast.makeText(LoginActivity.this, "iv_login_qq", Toast.LENGTH_SHORT).show();
                break;
        }
    }

}
